<template>
  <div>
    <el-container >
      <el-header class="homeHeader">
        <div class="homeHeader">Header</div>
        </el-header>
        <el-dropdown class="userInfo" @command = "commandHandler">
  <span class="el-dropdown-link">
   {{user.name}}<i><img :src="user.userFace" alt=""></i>
  </span>
  <el-dropdown-menu slot="dropdown">
    <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
    <el-dropdown-item command="setting">设置</el-dropdown-item>
    <el-dropdown-item command="logout">登陆</el-dropdown-item>
    
  </el-dropdown-menu>
  <div>
    <el-button icon="el-icon-bell" @click="goChats">聊天室</el-button>
    </el-dropdown>
      <el-container>
        <el-aside width="200px">
          <el-menu router unique-opened>
            <el-submenu :index="index+''" 
            v-for="(item,index) in routes " :key="index" 
            v-show="!item.hidden"
            >
              <template slot="title"
                ><i :class="item.iconcls"></i> 
                <span>{{item.name}}</span>
                </template>
              <el-menu-item-group>
                <el-menu-item :index="children.path"
                 v-for="(children,index) in item.children" :key="index">{{children.name}}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
            <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
  
</template>

<script>
export default {
   name:'Home',
   data(){
     return{
       user:JSON.parse(window.sessionStorage.getItem('user'))
     }
   },
   computed:{
     routes(){
       return this.$store.state.routes
     },
     user(){
       return this.$store.state.currentAdmin
     }
   },
   methods:{
     goChats(){
        this.$router.push('/chat')
     },
     commandHandler(command){
       if(command == 'logout'){
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.postRequest('/logout')
         //清空用户信息
         window.sessionStorage.removeItem('tokenStr')
         window.sessionStorage.removeItem('user')
         //清空菜单
         this.$store.commit('initRoutes',[])
         this.$router.push('/')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });    
        })
       }
       if(command == 'userinfo'){
         this.$router.push('/userinfo')
       }
     },
     computed:{
       routes(){
         return this.$store.state.routes
       },
       user(){
         return this.$store.state.currentAdmin
       }
     },
     
   }
};
</script>

<style>
.homeHeader{
  background:chartreuse;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.homeHeader .title{
  font-size: 30px;
  color: aqua;
}
.homeHeader .userInfo{
  cursor:pointer
}
</style>